package datastructure;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2019-11-19 下午1:59
 */
public class SimulateQueueWithStack {
    private Stack<Integer> stackA = new Stack<>();
    private Stack<Integer> stackB = new Stack<>();

    public static void main(String[] args) {
        SimulateQueueWithStack stackQueue = new SimulateQueueWithStack();
        stackQueue.enQueue(1);
        stackQueue.enQueue(2);
        stackQueue.enQueue(3);
        System.out.println(stackQueue.deQueue());
        System.out.println(stackQueue.deQueue());
        stackQueue.enQueue(4);
        System.out.println(stackQueue.deQueue());
        System.out.println(stackQueue.deQueue());

    }

    /**
     * 入栈
     *
     * @param element
     */
    public void enQueue(int element) {
        stackA.push(element);
    }

    /**
     * 出栈
     *
     * @return
     */
    public Integer deQueue() {
        if (stackB.isEmpty()) {
            if (stackA.isEmpty()) {
                return null;
            }

            transfer();
        }

        return stackB.pop();
    }

    /**
     * 将栈Ａ元素转移到栈Ｂ
     */
    private void transfer() {
        while (!stackA.isEmpty()) {
            stackB.push(stackA.pop());
        }
    }
}
